<?php


namespace app\validate;

use app\model\AdminUsers;
use think\Validate;



class UserValidate extends Validate
{
    protected $rule = [
        'name' => 'require|max:25',
        'username' => 'unique:admin_users,username^id|max:20',
        'avatar' => 'image|fileSize:2048000',
        'old_password' => 'requireWith:password|checkOldPassword',
        'password' => 'requireWith:old_password|confirm:confirm_password|min:6',
    ];

    protected $message = [
        'name.require' => '姓名不能为空',
        'username.unique' => '用户名已存在',
        'avatar.image' => '头像必须为图片文件',
        'old_password.requireWith' => '修改密码需要旧密码',
        'password.confirm' => '两次输入密码不一致',
    ];

    // 自定义验证规则：验证旧密码
    protected function checkOldPassword($value, $rule, $data)
    {
        $user = AdminUsers::find($data['id']);
        if (!password_verify($value, $user->password)) {
            return '旧密码不正确';
        }
        return true;
    }
}